package com.cloudcross.ssp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.model.AdgroupZone;
import com.cloudcross.ssp.service.IAdgroupZoneService;

@Service
public class AdgroupZoneService implements IAdgroupZoneService {
    @Autowired
    private GenericIBatisDao    dao;

    private final static String MAPPER_KEY = "adgroupZoneSqlMapper.";

    public void saveAdgroupZone(AdgroupZone adgroupZone) {
        dao.save(MAPPER_KEY + "addAdgroupZone", adgroupZone);
    }

    public boolean updateZoneIdByAdgroupId(Map<String, Object> map) {
        dao.save(MAPPER_KEY + "updateAdgroupZoneByAdgroupId", map);
        return true;
    }

    public boolean updateAdxZoneIdByAdgroupId(Map<String, Object> map) {
        dao.save(MAPPER_KEY + "updateAdgroupAdxZoneByAdgroupId", map);
        return true;
    }

    public List<Long> queryZoneIdByAdgroupId(Long adgroupId) {
        return dao.getList(MAPPER_KEY + "queryZoneIdByAdgroupId", adgroupId);
    }

    public void saveAdgroupZoneList(AdgroupZone adgroupZone, List<Long> zoneIdList) {
        dao.save(MAPPER_KEY + "addAdGroupZoneBatch",
                MapBuilder.create(HashMap.class).add("zoneIdList", zoneIdList).add("adgroupZone", adgroupZone).map());
    }

    public boolean copyAdgroupZone(long oldadgroupId, long newadgroupId) {
        Map<String, Object> condition = new HashMap<String, Object>();
        Date date = new Date();
        long supdated = date.getTime() / 1000;
        condition.put("oldadgroupId", oldadgroupId);
        condition.put("newadgroupId", newadgroupId);
        condition.put("supdated", supdated);
        dao.save(MAPPER_KEY + "copyAdgroupZone", condition);
        return true;
    }

    @Override
    public List<Integer> onlinePcAds() {
        return dao.getList(MAPPER_KEY + "onlinePcAds", null);
    }

    @Override
    public List<Integer> onlineAppAds() {
        return dao.getList(MAPPER_KEY + "onlineAppAds", null);
    }
    
    @Override
    public List<Integer> offLineAppAds() {
        return dao.getList(MAPPER_KEY + "offlineAppAds", null);
    }

    @Override
    public List<Integer> offLinePcAds() {
        return dao.getList(MAPPER_KEY + "offlinePcAds", null);
    }
}
